package com.wdb007.venus.web.api.controller;

import org.springframework.util.ObjectUtils;

import com.wdb007.venus.svc.facade.constant.RspCodeEnum;
import com.wdb007.venus.svc.facade.exception.BizException;
import com.wdb007.venus.svc.facade.exception.SysException;
import com.wdb007.venus.web.api.model.BaseResponse;

public class BaseController {

	/**
	 * 默认返回一个成功的对象
	 * @return
	 */
	protected BaseResponse success() {
		BaseResponse baseResponse = new BaseResponse();
		baseResponse.setResult(1);
		baseResponse.setCode(0);
		baseResponse.setMessage("操作成功");
		return baseResponse;
	}
	/**
	 * 操作异常
	 * @param e
	 * @return
	 */
	protected BaseResponse failed(Exception e) {
		BaseResponse baseResponse = new BaseResponse();
		baseResponse.setResult(0);
		if(e instanceof BizException) {
			BizException bizRuntimeException = (BizException) e;
			if(bizRuntimeException.getErrorCode() == 0) {
				baseResponse.setResult(1);
			}else if(bizRuntimeException.getErrorCode() == 1010080002) {
				baseResponse.setResult(1);
				baseResponse.setCode(0);
				baseResponse.setMessage("操作成功");
			}else {
				baseResponse.setCode(bizRuntimeException.getErrorCode());
				baseResponse.setMessage(bizRuntimeException.getErrorMsg());
			}
		}else if(e instanceof SysException) {
			SysException sysRuntimeException = (SysException) e;
			baseResponse.setCode(sysRuntimeException.getErrorCode());
			baseResponse.setMessage(sysRuntimeException.getErrorMsg());
		}else {
			baseResponse.setCode(1010080099);
			baseResponse.setMessage("系统异常，请联系管理员");
		}
		return baseResponse;
	}
	
	protected void checkIsNull(Object obj, String key) throws BizException {
		if(ObjectUtils.isEmpty(obj) || "undefined".equals(obj) || "null".equals(obj)) {
			throw new BizException(RspCodeEnum.PARAM_CHECK_IS_NULL.setRspMessage(key));
		}
	}
	
	protected boolean isEmpty(Object obj){
		if(ObjectUtils.isEmpty(obj) || "null".equals(obj) || "undetined".equals(obj)) {
			return true;
		}
		return false;
	}
}
